package com.isesol.arch.common.uitil.redis;

import com.isesol.arch.common.util.properties.*;
import com.wandoulabs.jodis.*;
import org.apache.commons.lang3.*;
import org.slf4j.*;
import org.springframework.stereotype.*;
import redis.clients.jedis.*;

import javax.annotation.*;
import java.io.*;

/**
 * RedisUtil
 *
 * @author Peter Zhang
 */
@Component
public class RedisUtil {

    protected Logger LOGGER = LoggerFactory.getLogger(getClass());

    JedisResourcePool jedisPool;

    @PostConstruct
    public void init() throws IOException {

        String zkAddr = PropertyFileUtil.get("zk.addr");
        String jodisZkProxyDir = PropertyFileUtil.get("jodis.zk.proxy.dir");
        String redisServerType = PropertyFileUtil.get("redis.server.type");
        if (StringUtils.isNotBlank(redisServerType) && "test".equals(redisServerType)) {

            jedisPool= new MockJedisPool();

        }else{

            jedisPool = RoundRobinJedisPool.create()
                    .curatorClient(zkAddr, 30000).zkProxyDir(jodisZkProxyDir).build();
        }

    }

    @PreDestroy
    public void destroy() {

        try {

            jedisPool.close();

            LOGGER.info("jedisPool closed");

        } catch (java.io.IOException e) {

            LOGGER.error("jedisPool close error", e);
        }
    }

    public void set(String key, String value) throws Exception {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.set(key, value);

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();
            }
        }
    }

    public void sadd(String key, String value) throws Exception {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.sadd(key,value);

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();
            }
        }
    }

    public long scard(String key) throws Exception {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();

            return jedis.scard(key);

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();
            }
        }
    }


    public String get(String key) throws Exception {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            return jedis.get(key);

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();
            }
        }

    }

    public long incr(String key) throws Exception {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            long inc = jedis.incr(key);

            return inc;

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();

            }
        }
    }

    public long decr(String key) throws Exception {

        Jedis jedis = null;

        try {

            jedis = jedisPool.getResource();

            long decr = jedis.decr(key);

            return decr;

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();

            }
        }
    }

    public void del(String key) throws Exception {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.del(key);

        } catch (Exception e) {

            LOGGER.error("jedis  error", e);

            throw e;

        } finally {

            if (null != jedis) {

                jedis.close();
            }
        }
    }


}
